/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月7日         Root.Lu
 */

package com.coocaa.x.service.litecontent.providers.setting.host;

import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.os.SystemProperties;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.CoocaaApplication.CoocaaVersion;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecontent.LiteContentProvider;
import com.coocaa.x.service.litecontent.LiteContentProvider.LiteContentProviderHost;
import com.coocaa.x.service.litecontent.LiteContentProvider.ResourceHandler;
import com.coocaa.x.service.litecontent.data.ResultPackage;
import com.coocaa.x.service.litecontent.data.setting.com_coocaa_launcher.RP_Backkey_To_Exit;
import com.coocaa.x.service.litecontent.data.setting.com_coocaa_launcher.RP_Backkey_To_Exit.Backkey_To_Exit_Item;
import com.coocaa.x.service.litecontent.data.setting.com_coocaa_launcher.RP_Current_Launcher;
import com.coocaa.x.service.litecontent.data.setting.com_coocaa_launcher.RP_Current_Launcher.Current_Launcher_Item;

public class SettingHost_Com_Coocaa_Launcher extends LiteContentProviderHost {
    private static SettingHost_Com_Coocaa_Launcher instance = null;

    public static synchronized void create(Context context, LiteContentProvider provider) {
        if (instance == null)
            instance = new SettingHost_Com_Coocaa_Launcher(context, provider);
    }

    private static final String MY_HOST = "com.coocaa.launcher";

    public SettingHost_Com_Coocaa_Launcher(Context context, LiteContentProvider provider) {
        super(context, provider);
    }

    @Override
    public String myHost() {
        return MY_HOST;
    }

    @SuppressWarnings("unused")
    // 2.1.1、桌面返回键退出
    private ResourceHandler backkey_to_exit = new ResourceHandler(this, "/backkey_to_exit") {

        @Override
        public ResultPackage call(Uri uri) {
            RP_Backkey_To_Exit rp = new RP_Backkey_To_Exit();
            String action = uri.getQueryParameter("action");
            if (action.equals("get")) {
                rp.resource.add(new Backkey_To_Exit_Item(true));
            } else if (action.equals("set")) {

            }
            return rp;
        }
    };

    @SuppressWarnings("unused")
    // 2.1.2、桌面返回键退出
    private ResourceHandler current_launcher = new ResourceHandler(this, "/current_launcher") {
        @Override
        public ResultPackage call(Uri uri) {
            RP_Current_Launcher rp = new RP_Current_Launcher();
            String action = uri.getQueryParameter("action");
            if (action.equals("get")) {
                CoocaaVersion cv = null;
                try {
                    cv = CoocaaApplication.getCoocaaVersion();
                } catch (Exception e) {
                    rp.error_code = -1;
                    rp.error_msg = "";
                }
                if (cv != null) {
                    String pkgname = null, activity = null;
                    if (cv.major == 5) {
                        try {
                            ComponentName cn = XServiceManager.getLiteCCConnectorService()
                                    .getCurrentLauncher();
                            pkgname = cn.getPackageName();
                            activity = cn.getClassName();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else if (cv.major == 4) {
                        pkgname = SystemProperties.get("persist.service.homepage.pkg");
                        activity = SystemProperties.get("persist.service.homepage.cls");
                    }
                    if (pkgname != null && activity != null && !pkgname.equals("")
                            && !activity.equals("")) {
                        Current_Launcher_Item item = new Current_Launcher_Item(pkgname, activity);
                        rp.resource.add(item);
                    } else {
                        rp.error_code = -1;
                        rp.error_msg = "";
                    }
                } else {
                    rp.error_code = -1;
                    rp.error_msg = "";
                }
            }
            return rp;
        }
    };
}
